package org.ar.xmling.dom.processors;

import java.util.List;

import org.ar.xmling.dom.XMLTag;
import org.ar.xmling.dom.XMLTagAttribute;

public class XMLTagProcessor {
  protected List<XMLTag> tags;

  public XMLTag findTagByAttributeName(String name) {
    for (XMLTag tag : tags) {
      if (tag.findAttributeByName(name) != null) {
        return tag;
      }
    }
    return null;
  }

  public XMLTag findTagByAttributeValue(String value) {
    for (XMLTag tag : tags) {
      if (tag.findAttributeByValue(value) != null) {
        return tag;
      }
    }
    return null;
  }

  public XMLTag findTagByAttributePair(String name, String value) {
    for (XMLTag tag : tags) {
      XMLTagAttribute attr = tag.findAttributeByName(name);
      if (attr != null && attr.getValue().equals(value)) {
        return tag;
      }
    }
    return null;
  }

  public XMLTag findTagByName(String name) {
    for (XMLTag tag : tags) {
      if (tag.getName().equals(name)) {
        return tag;
      }
    }
    return null;
  }

  public XMLTag findTagByValue(String value) {
    for (XMLTag tag : tags) {
      if (tag.getValue().equals(value)) {
        return tag;
      }
    }
    return null;
  }

  public XMLTag findTagByNameAndValue(String name, String value) {
    for (XMLTag tag : tags) {
      if (tag.getName().equals(name) && tag.getValue().equals(value)) {
        return tag;
      }
    }
    return null;
  }

  public XMLTag findTagByInnerTag(XMLTag tag) {
    for (XMLTag t : tags) {
      if (t.tags().contains(tag)) {
        return t;
      }
    }
    return null;
  }

  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((tags == null) ? 0 : tags.hashCode());
    return result;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj) return true;
    if (obj == null) return false;
    if (getClass() != obj.getClass()) return false;
    XMLTagProcessor other = (XMLTagProcessor) obj;
    if (tags == null) {
      if (other.tags != null) return false;
    } else if (!tags.equals(other.tags)) return false;
    return true;
  }


}
